<template>
  <a-input v-bind="$attrs" :class="prefixCls" :size="size" :value="state">
    <template #prefix>
      <Icon :icon="icon" v-if="showIcon" />
    </template>
    <template #suffix>
      <CaptchaImg :beforeStartFunc="getCaptchaApi" />
    </template>
    <!-- <template #addonAfter>
      <CaptchaImg :beforeStartFunc="getCaptchaApi" />
    </template> -->
    <!-- <template #[item]="data" v-for="item in Object.keys($slots).filter((k) => k !== 'addonAfter')">
      <slot :name="item" v-bind="data || {}"></slot>
    </template> -->
  </a-input>
</template>
<script lang="ts">
  import { defineComponent, PropType } from 'vue';

  import CaptchaImg from './CaptchaImg.vue';
  import Icon from '@/components/Icon/Icon.vue';

  import { useDesign } from '@/hooks/web/useDesign';

  import { useRuleFormItem } from '@/hooks/component/useFormItem';

  const props = {
    value: { type: String },
    size: { type: String, validator: (v: string) => ['default', 'large', 'small'].includes(v) },
    getCaptchaApi: {
      type: Function as PropType<() => Promise<string>>,
      default: null,
    },
    icon: { type: String },
  };

  export default defineComponent({
    name: 'CaptchaInput',
    components: { CaptchaImg, Icon },
    inheritAttrs: false,
    props,
    setup(props) {
      const { prefixCls } = useDesign('captcha-input');
      const [state] = useRuleFormItem(props);
      const showIcon = props.icon !== undefined && props.icon !== null && props.icon !== '';

      return { prefixCls, state, showIcon };
    },
  });
</script>
<style lang="less">
  @prefix-cls: ~'@{namespace}-captcha-input';

  .@{prefix-cls} {
    .ant-input-group-addon {
      padding-right: 0;
      border: none;
      background-color: transparent;

      // img {
      //   width: 100px;
      //   height: 40px;
      //   max-width: none;
      //   cursor: pointer;
      //   object-fit: fill;
      // }
    }
  }
</style>
